package Q5;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;

public class ServerThread extends Thread {
    private Socket s;
    private ArrayList<Socket> socketList;

    public ServerThread(Socket s, ArrayList<Socket> socketList) {
        this.s = s;
        this.socketList = socketList;
    }

    @Override
    public void run() {
        ObjectInputStream objectInputStream = null;
        ObjectOutputStream objStream1 = null;
        try {
            objectInputStream = new ObjectInputStream(s.getInputStream());
            while (true) {
                System.out.println("Server等待数据的到来");
                Message msg = (Message) objectInputStream.readObject();
                if ("chat".equals(msg.getMessageType())) {
                    // bye 下线
                    if ("bye".equals(msg.getData())) {
                        System.out.println("用户" + msg.getUser() + "下线");
                        socketList.remove(s);
                        break;
                    }
                    for (Socket ts : socketList) {
                        if (ts != s) {
                            objStream1 = new ObjectOutputStream(ts.getOutputStream());
                            objStream1.writeObject(msg);
                        }
                    }
                } else if ("file".equals(msg.getMessageType())){
                    String filename = msg.getData();
                    String name = msg.getUser();
                    System.out.println("服务器收到文件请求" + filename + "来自" +name);
                    // recieved
/*                    File file = new File("./code/Q5/Server", msg.getData());
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    Util.transfer(fileOutputStream, s.getInputStream());//TODO*/
                    File dict = new File("./code/Server/");
                    do {
                        if (!dict.isDirectory()) {
                            dict.mkdir();
                        }
                        msg = Util.SocketMessagetoFile(objectInputStream, msg, dict.getPath());
                    } while (null != msg);
                    System.out.println("文件接收完成");
                    // send
                    for (Socket ts : socketList) {
                        if (ts != s) {
                            FileInputStream fileInputStream = new FileInputStream(new File(dict, filename));
                            /*Util.transfer(ts.getOutputStream(), fileInputStream); //TODO*/
                            objStream1 = new ObjectOutputStream(ts.getOutputStream());
                            Util.filetoSocketMessageStream(objStream1,fileInputStream,filename,name);
                            fileInputStream.close();
                        }
                    }
                    System.out.println("文件发送完成");
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                assert objectInputStream != null;
                objectInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                assert objStream1 != null;
                objStream1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
